package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.annotion.Permission;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.ConstantFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import com.sxfq.loansupermarket.backsystem.core.log.LogObjectHolder;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroUser;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.DateUtil;
import com.sxfq.loansupermarket.backsystem.core.util.MyDateUtils;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.system.factory.UserFactory;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.AchievementsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchAchievementsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.IAchievementsInfoService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@SuppressWarnings("unused")
@RequestMapping("/achievements")
public class AchievementsController extends BaseController {

    @Autowired
    private IAchievementsInfoService Service;

    @Autowired
    UserMapper userMapper;

    private String PREFIX = "/theTest/";

    /**
     * 跳转到订单信息
     */
    @RequestMapping("")
    public String achievementsIndex() {
        return PREFIX + "achievements.html";
    }

    /**
     * 跳转到修改部门
     */
//    @Permission
    @RequestMapping("/addAchievementsRemark/{userForTestInfoId}")
    public String addAchievementsRemark(@PathVariable String userForTestInfoId, Model model) {
        try {
            AchievementsInfo achievementsInfo = Service.selectOneById(userForTestInfoId);
            model.addAttribute("remark", achievementsInfo.getRemark());
            model.addAttribute("userForTestInfoId", userForTestInfoId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return PREFIX + "addAchievementsRemark.html";
    }

    @RequestMapping(value = "/addRemark")
    @ResponseBody
    public int update(@Valid AchievementsInfo entity, BindingResult result) throws Exception {
        try {
            Service.UpdateWithInfo(entity);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 跳转到考试成绩列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchAchievementsInfo searchAchievementsInfo, HttpServletRequest request) {
        Page<AchievementsInfo> page = new PageFactory<AchievementsInfo>().defaultPage();
        try {
            SearchAchievementsInfo searchInfo = createSearchEntity(request);
            List<AchievementsInfo> list = Service.selectPageListByQueryGetMap(page, searchInfo);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    public SearchAchievementsInfo createSearchEntity(HttpServletRequest request) {
        SearchAchievementsInfo searchInfo = new SearchAchievementsInfo();
        String theTestName = request.getParameter("theTestName");
        String userName = request.getParameter("userName");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        if (!CommUtils.isNull(theTestName)) {
            searchInfo.setTheTestName(theTestName);
        }
        if (!CommUtils.isNull(userName)) {
            searchInfo.setUserName(userName);
        }
        if (!CommUtils.isNull(startTime)) {
            searchInfo.setStartTime(startTime);
        }
        if (!CommUtils.isNull(endTime)) {
            Date parse = DateUtil.parse(endTime, "yyyy-MM-dd");
            Date date1 = MyDateUtils.addDays(parse, 1);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            searchInfo.setEndTime(sd.format(date1));
        }
        return searchInfo;
    }

    /**
     * 上传图片(上传到项目的webapp/static/img)
     */
    @RequestMapping(method = RequestMethod.POST, path = "/uploadtx")
    @ResponseBody
    public String uploadtx(@RequestPart("file") MultipartFile picture) {
//        String realPath = "D:\\wwwroot\\yiyayl\\wwwroot\\photo";
        String realPath = "D:\\20180409\\apache-tomcat-8.0.30\\webapps\\ROOT\\touxiang";
//        String FileInput = realPath + "\\" + picture.getOriginalFilename();
//        String httpUrl = "http://yiyayl.com/photo/";
        String httpUrl = "http://47.98.150.186/touxiang/";
        try {
            FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(realPath, picture.getOriginalFilename()));
            UserDto user = new UserDto();
            ShiroUser shiroUser = ShiroKit.getUser();
            user.setId(shiroUser.getId());
            user.setAvatar(httpUrl + "\\" + picture.getOriginalFilename());
            this.userMapper.updateById(UserFactory.createUser(user));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return realPath;
    }

}
